﻿using AutoMapper;
using Microsoft.EntityFrameworkCore;
using StudentEnrollment.Api.DTOs.Course;
using StudentEnrollment.Data;
using StudentEnrollment.Data.Contracts;

namespace StudentEnrollment.Api.Endpoints
{
    public static class CourseEndpoints
    {
        public static void MapCourseEndpoints(this IEndpointRouteBuilder router)
        {
            router.MapGet("/api/Course", async (ICourseRepository db, IMapper mapper) =>
            {
                var courses = await db.GetAllAsync();
                var data = mapper.Map<List<CourseDto>>(courses);
                return data;
            })
                .WithTags(nameof(Course))
                .WithName("GetAllCourses")
                .Produces<List<CourseDto>>(StatusCodes.Status200OK);



            router.MapGet("/api/Course/{id}", async (int id, ICourseRepository db, IMapper mapper) =>
            {
                return await db.GetAsync(id) is Course model
                ? Results.Ok(mapper.Map<CourseDto>(model))
                    : Results.NotFound();
            })
                .WithTags(nameof(Course))
                .WithName("GetCourseById")
                .Produces<CourseDto>(StatusCodes.Status200OK)
                .Produces(StatusCodes.Status404NotFound);



            router.MapGet("/api/Course/GetStudents/{id}", async (int Id, ICourseRepository db, IMapper mapper) =>
            {
                return await db.GetStudentList(Id) is Course model
                ? Results.Ok(mapper.Map<CourseDetailsDto>(model))
                    : Results.NotFound();
            })
            .WithTags(nameof(Course))
            .WithName("GetCourseDetailsById")
            .Produces<CourseDetailsDto>(StatusCodes.Status200OK)
            .Produces(StatusCodes.Status404NotFound);



            router.MapPut("/api/Course/{id}", async (int Id, CourseDto courseDto, ICourseRepository db, IMapper mapper) =>
            {
                var foundModel = await db.GetAsync(Id);
                if (foundModel is null)
                {
                    return Results.NotFound();
                }

                // update model properties here 
                mapper.Map(courseDto, foundModel);
                await db.UpdateAsync(foundModel);

                return Results.NoContent();
            })
                .WithTags(nameof(Course))
                .WithName("UpdateCourse")
                .Produces(StatusCodes.Status404NotFound)
                .Produces(StatusCodes.Status204NoContent);


            router.MapPost("/api/Course", async (CreateCourseDto createCourseDto, ICourseRepository db, IMapper mapper) =>
            {
                var course = mapper.Map<Course>(createCourseDto);
                await db.AddAsync(course);
                return Results.Created($"/Courses/{course.Id}", course);
            })
                .WithTags(nameof(Course))
                .WithName("CreateCourse")
                .Produces<Course>(StatusCodes.Status201Created);




            router.MapDelete("/api/Course/{id}", async (int Id, ICourseRepository db) =>
            {
                return await db.DeleteAsync(Id) ? Results.NoContent() : Results.NotFound();
            })
                .WithTags(nameof(Course))
                .WithName("DeleteCourse")
                .Produces<Course>(StatusCodes.Status200OK)
                .Produces(StatusCodes.Status404NotFound);
        }

    }
}
